﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：PurchaseOrderUpdate.cs
    // 文件功能描述：编辑采购单明细信息 新增采购明细
    // 创建标识：吕海乐2009-04-29
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.ProcureManage
{
    public partial class PurchaseOrderUpdateAdd : BasePage
    {
        public PurchaseOrderUpdateAdd()
        {
            this.ModuleNo = Module.采购单管理.ToString();
        }

        IBLL_Distribution id = Factory.BLL_Distribution();
        IBLL_Purchase ip = Factory.BLL_Purchase();
        IBLL_HospitalProcureCatalog hpcCtrl = Factory.BLL_HospitalProcureCatalog();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetBind();
                //Check();
            }
        }

        private void SetBind()
        {

            int totalCount = 0;
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;

            //绑定所有符合条件的目录（采购目录启用，医疗机构启用了该采购目录）
            Model.Query.HospitalProcureCatalog hospitalProcureCatalog = GetCondition();

            IList<Model.View.HospitalProcureCatalog> hosiptalCatalogList = hpcCtrl.GetHospitalProcureCatalog(hospitalProcureCatalog, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;

            
            gvwProduceCatalog.DataSource = hosiptalCatalogList;
            gvwProduceCatalog.DataBind();
        }


        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        private Model.Query.HospitalProcureCatalog GetCondition()
        {
            Model.Query.HospitalProcureCatalog hospitalProcureCatalog = new HZ.DrugTrade.Model.Query.HospitalProcureCatalog();
            hospitalProcureCatalog.HospitalID = Request.QueryString["hID"];
            hospitalProcureCatalog.SortFields = "ProductName asc";

            hospitalProcureCatalog.IsUsing = true;
            hospitalProcureCatalog.Hospital_IsUsing = true;


            //获取采购单中以后的采购目录编号
            int totalCount;
            Model.Query.PurchaseOrderDetail qPurchaseOrderDetail = new HZ.DrugTrade.Model.Query.PurchaseOrderDetail();
            qPurchaseOrderDetail.OrderID = Request.QueryString["orderID"];
            qPurchaseOrderDetail.HospitalID =  Request.QueryString["hID"];
          
            IList<Model.View.PurchaseOrderDetail> purchaseOrderDetailList = ip.GetPurchaseOrderDetailByWhere(qPurchaseOrderDetail, 1, 0, out totalCount);
            string procureCatalogIDs = StringUtility.GetModelFieldArrayString(purchaseOrderDetailList, "ProcureCatalogID", ",", true);
            if (!string.IsNullOrEmpty(procureCatalogIDs))
            {
                hospitalProcureCatalog.Append = "ProcureCatalogID Not in (" + procureCatalogIDs + ")";
            }

            return hospitalProcureCatalog;
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            SetBind();
            //Check();
        }


        /// <summary>
        /// 添加采购明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            List<PurchaseOrderDetail> myList = new List<PurchaseOrderDetail>();
            for (int i = 0; i < gvwProduceCatalog.Rows.Count; i++)
            {
                System.Web.UI.HtmlControls.HtmlInputCheckBox chk = (System.Web.UI.HtmlControls.HtmlInputCheckBox)gvwProduceCatalog.Rows[i].FindControl("checkbox1");
                TextBox txtPurchaseCount = (TextBox)gvwProduceCatalog.Rows[i].FindControl("txtCount");
                Label lblPrcie = (Label)gvwProduceCatalog.Rows[i].FindControl("lblPrice");
                if (chk.Checked == true)
                {
                    //判断采购数量是否为空
                    if (string.IsNullOrEmpty(txtPurchaseCount.Text.Trim()))
                    {
                        HZ.Web.WebUtil.Alert("采购数量不能为空！",this);
                        txtPurchaseCount.Focus();
                        return;
                    }
                    PurchaseOrderDetail purchaseOrderDetail = new PurchaseOrderDetail();
                    purchaseOrderDetail.OrderID = Request.QueryString["OrderID"];
                    purchaseOrderDetail.Price = new HZ.Common.ConvertHelper(lblPrcie.Text).ToDecimal;
                    purchaseOrderDetail.ProcureCatalogID = gvwProduceCatalog.DataKeys[i].Values[0].ToString();
                    purchaseOrderDetail.PurchaseCount = new HZ.Common.ConvertHelper(txtPurchaseCount.Text.Trim()).ToInt32;
                    purchaseOrderDetail.Amount = purchaseOrderDetail.Price * purchaseOrderDetail.PurchaseCount;
                    purchaseOrderDetail.CompanyID_PS = gvwProduceCatalog.DataKeys[i].Values[1].ToString();
                    purchaseOrderDetail.FlowID = "1";
                    myList.Add(purchaseOrderDetail);
                }
            }

            int flag = ip.AddPurchaseOrderDetail_Batch(myList);
            if (flag == 1)
            {
                SetBind();
               // Check();
            }
            else
            {
                PageError("添加失败！", "");
            }
        }

        protected void btnFinish_Click(object sender, EventArgs e)
        {
            Response.Redirect("PurchaseOrderUpdate.aspx?orderID=" + Request.QueryString["OrderID"] + "");
        }

        /// <summary>
        /// 检查此产品是否已经被采购
        /// </summary>
        //private void Check()
        //{
        //    int totalCount = 0;
        //    HZ.DrugTrade.Model.Query.PurchaseOrderDetail qPurchaseOrderDetail = new HZ.DrugTrade.Model.Query.PurchaseOrderDetail();
        //    qPurchaseOrderDetail.OrderID = Request.QueryString["orderID"];
        //    qPurchaseOrderDetail.SortFields = "PlanTime DESC";
        //    IList<Model.View.PurchaseOrderDetail> purchaseOrderDetailList = ip.GetPurchaseOrderDetailByWhere(qPurchaseOrderDetail, 1, 0, out totalCount);
        //    for (int i = 0; i < gvwProduceCatalog.Rows.Count; i++)
        //    {
        //        for (int j = 0; j < purchaseOrderDetailList.Count; j++)
        //        {
        //            string procureCatalogID = gvwProduceCatalog.DataKeys[i].Values[0].ToString();
        //            string companyID = gvwProduceCatalog.DataKeys[i].Values[1].ToString();
        //            if (purchaseOrderDetailList[j].ProcureCatalogID.Equals(procureCatalogID) && purchaseOrderDetailList[j].CompanyID_PS.Equals(companyID))
        //            {
        //                System.Web.UI.HtmlControls.HtmlInputCheckBox chk = (System.Web.UI.HtmlControls.HtmlInputCheckBox)gvwProduceCatalog.Rows[i].FindControl("checkbox1");
        //                chk.Disabled = true;
        //                break;
        //            }
        //        }
        //    }
        //}
    }
}
